$(document).ready(function () {
    var url = window.location.href;

    var segment = url.split('/');

    for (var i = 0; i < segment.length; i++) {
        if (segment[i].trim() === '') {
            segment.splice(i, 1);
        }
    }
    segment.splice(0, 1);
    segment.splice(0, 1);
    for (var i = 0; i < segment.length; i++) {
        if (segment[i].indexOf('?') > 0) {
            segment[i] = segment[i].split('?')[0];
        }
    }

    var ids = $('.idenclass');

    ids.each(function (index) {
        var path = $(this).attr('href');
        if (path === 'undefined') {
            return false;
        }
        var pathsegment = path.split('/');

        if (pathsegment[0] ==='.' || pathsegment[0] === '..') {
            pathsegment.splice(0, 1);
        }

        for (var i = 0; i < segment.length; i++) {
            if (pathsegment[2] === segment[i]) {
                $(this).addClass('active');
                $(this).parent().addClass('in');
                return false;
            }
        }
    });

    /**
     * 年月日
     */
    $('.date-picker').datepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        todayBtn: 'linked',  //今天按钮
        autoclose: true,
        todayHighlight: true
    });

    /**
     * bootstrap-table 查询条件序列化
     * @returns {{}}
     */
    $.fn.serializeJsonObject = function() {
        var json = {};
        var form = this.serializeArray();
        $.each(form, function() {
            if (json[this.name]) {
                if (!json[this.name].push) {
                    json[this.name] = [ json[this.name] ];
                }
                json[this.name].push( );
            } else {
                json[this.name] = this.value || '';
            }
        });
        return json;
    }
});

/**
 * 获取所有导航资源
 */
function ajaxmenu() {
    $.ajax({
        url : "/system/menu/leftMenu",
        type : "post",
        cache : false,
        async : false,
        scriptCharset : "utf-8",
        dataType : "json",
        success : function(_json) {
            if(_json.status===0){
                locache.set("menuData",_json.data);
                var menuData = locache.get("menuData");
                buildModule(menuData);
            }
        }
    });
}

function buildModule(data) {
    if (data != null && typeof (data) !== "undefined") {
        var htmlStr = '';
        /*处理/update时丢了菜单 状态*/
        var _url=window.location.pathname;
        if(_url.indexOf('/update/pre')!==-1){
            _url=_url.substring(0,_url.indexOf("/update/pre"))+"/list";
        }
        /*处理/update时丢了菜单 状态*/
        var childrenMenuList = null;
        for ( var i = 0; i < data.length; i++) {
            var url = data[i].pageurl;
            var tmpData = data[i]['leaf'][0];
            while(!!tmpData){
                url = tmpData.pageurl;
                tmpData = tmpData['leaf'][0];
            }
            var tmpPid = locache.get("currentPagePid");
            var menuIcon_df="&#xe63c;";
            if(data[i].menuIcon!=null){
                menuIcon_df=data[i].menuIcon;
            }
            if((tmpPid === data[i].id) || (!(!!tmpPid) && i===0)){//url中有第一个菜单的键值
                htmlStr += '<li id="pmenu'+data[i].id+'" class="layui-nav-item layui-this"><a href="javascript:void(0);" data-pid="'+data[i].id+'" data-action="'+url+'"><i class="layui-icon">'+menuIcon_df+'</i><cite>'+data[i].name+'</cite></a></li>';
                childrenMenuList = data[i]['leaf'];
                $("ul.site-demo-title").prepend('<li class="layui-this">\
	             		<i class="layui-icon">&#xe630;</i>\
	             		<span class="layui-breadcrumb" style="visibility: visible;" id="neckNavi">\
						  <a><cite>'+data[i].name+'</cite></a><span class="layui-box">&gt;</span>\
						</span>\
             		</li>');
            }else{
                htmlStr += '<li id="pmenu'+data[i].id+'" class="layui-nav-item"><a href="javascript:void(0);" data-pid="'+data[i].id+'" data-action="'+url+'"><i class="layui-icon">'+menuIcon_df+'</i><cite>'+data[i].name+'</cite></a></li>';
            }
        }
        $("#naviHeaderMenu").html(htmlStr);
        $("#menu").html(getParentModule(childrenMenuList));
    }
}